package com.byxy.managerial.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.byxy.managerial.entity.Department;
import com.byxy.managerial.service.DepartmentService;

public class DepartmentAddAction {
	private String dname;
	private String principal;
	private String depint;

	@Resource
	private DepartmentService departmentService;

	public void addDepart() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		Department department = new Department(dname, depint, principal);	
		boolean isAdd = departmentService.save(department);
		if (isAdd) {
			map.put("isAdd", isAdd);
		}else {
			map.put("isAdd", "该部门已存在！");
		}
		PrintWriter pw = response.getWriter();
		String str = JSON.toJSONString(map);
		pw.write(str);
		pw.flush();
		pw.close();
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public String getPrincipal() {
		return principal;
	}

	public void setPrincipal(String principal) {
		this.principal = principal;
	}

	public String getDepint() {
		return depint;
	}

	public void setDepint(String depint) {
		this.depint = depint;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	

}
